<?php
namespace App\Admin\Controllers;

use App\Models\MemberAddress;
use App\Models\Member;
use App\Models\Region;
use SmallRuralDog\Admin\Components\Attrs\SelectOption;
use SmallRuralDog\Admin\Components\Form\Checkbox;
use SmallRuralDog\Admin\Components\Form\InputNumber;
use SmallRuralDog\Admin\Components\Form\Select;
use SmallRuralDog\Admin\Components\Form\Upload;
use SmallRuralDog\Admin\Components\Grid\Image;
use SmallRuralDog\Admin\Controllers\AdminController;
use SmallRuralDog\Admin\Form;
use SmallRuralDog\Admin\Grid;
use SmallRuralDog\Admin\Components\Form\Cascader;
use SmallRuralDog\Admin\Components\Grid\Tag;

class MemberAddressController extends AdminController
{
    public function grid()
    {
        $grid = new Grid(new MemberAddress());
        $grid->defaultSort('id', 'desc');
        $grid->column('id', '编号')->sortable()->width(100);
        $grid->column('member.nickname', '会员')->width(100);
        $grid->column('consignee', '联系人')->width(100);
        $grid->column('mobile', '联系电话')->width(150);
        // $grid->column('country', '国家')->width(100);
        $grid->column('province_name', '省')->width(100);
        $grid->column('city_name', '市')->width(100);
        $grid->column('district_name', '区')->width(100);
        $grid->column('address', '详细地址')->width(200);
        $grid->column('default', "是否默认")->width(100)->align("center")->customValue(function ($row, $value) {
            return $value == 1 ? "是" : "否";
        })->component(Tag::make()->type(["是" => "success", "否" => "不默认"]));
        $grid->column('created_at', '注册时间')->width(150);

        $grid->toolbars(function (Grid\Toolbars $toolbars) {

        });

        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('member_id', '会员')->component(Select::make()->options(function () {
                return Member::query()->get()->map(function ($item) {
                    return SelectOption::make($item->member_id, $item->nickname);
                })->all();
            }));
        });

        $grid->toolbars(function (Grid\Toolbars $toolbars) {
            $toolbars->createButton()->content("添加收货地址");
        });

        $grid->quickSearch('consignee', 'mobile');
        $grid->quickSearchPlaceholder("请输入联系人或联系电话");

        return $grid;
    }

    public function form($isEdit = false)
    {
        $form = new Form(new MemberAddress());
        $form->item('member_id', "会员")->required()->serveRules("min:1")->component(Select::make(null)->filterable()->options(function () {
            return Member::query()->orderBy('created_at')->get()->map(function ($item) {
                return SelectOption::make($item->member_id, $item->username)->avatar(admin_file_url($item->headimgurl))->desc(strtoupper($item->nickname));
            })->all();
        }));
        $form->item('consignee', '联系人')->required()->inputWidth(5);
        $form->item('mobile', '联系电话')->required()->inputWidth(5);
        $form->item("region", "省市区")->required()->displayComponent(function () {
            $region = new Region();
            // $allNodes = $region->toTree(); // 从国家开始
            $allNodes = $region->buildNestedArray([], 1); // 从省开始
            return Cascader::make()->options($allNodes)->value("code")->label("name")->expandTrigger("hover");
        });
        $form->item('address', '详细地址')->required()->inputWidth(8);
        $form->item('default', '默认')->required()->component(Checkbox::make(1, "默认"));

        $form->saving(function (Form $form)
        {
            // dd($form->region, $form->default);
            if ($form->region)
            {
                // $form->country = $form->region[0];
                $form->province = $form->region[0];
                $form->city = $form->region[1];
                $form->district = $form->region[2];
            }

            if ($form->default)
            {
                // 取消所有地址的默认
                MemberAddress::where('member_id', $form->member_id)->update(['default'=>0]);
            }
        });

        return $form;
    }
}
